/****************************************************************
*
* www.payitforward.edu.vn
*
****************************************************************/
/****************************************************************
*
* PIC Training Course
*
****************************************************************/
/****************************************************************
*
*    Module        : game.c
*    Description  : use a bar to catch a point
*                         
*    Tool            : HI-TECH PIC
*    Chip            : 16F887
*     History       : 26/07/2012
*
*    Author        : Cu Xuan Duc vs Nguyen Tran Duc Duy
*    Notes          :
*
*
****************************************************************/
/****************************************************************
* IMPORT
****************************************************************/
#include <htc.h> // Thu vien htc compiler
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & BOREN_OFF & IESO_OFF & FCMEN_OFF & LVP_OFF & DEBUG_OFF);  //1st config. Word
__CONFIG(BOR4V_BOR21V); //2st config. Word 
#define _XTAL_FREQ 	4000000 // khai bao tan so thach anh
#define trai RE0
#define phai RE1
/****************************************************************
* EXTERN
****************************************************************/
/*none...*/
/*****************************************************************
GLOBAL VARIABLE
******************************************************************/
int dem;
char cham,diem,kq,d,lv, c,i,thanh,j,hang;
/*****************************************************************
* ROUTINES
******************************************************************/
/*config port and set start value*/
void system_init()
{
	diem=0;
	kq=0;
	j=0;
	srand(TMR0); 
	ANSEL =0;
	ANSELH=0;
	TRISE = 0b011; 
	TRISD = 0x00;			
	TRISC = 0X00;
	TRISB = 0x00;
	TRISA = 0b000000;
	thanh = 0x38;
	hang = 0x01;
	i=0;
	cham=rand()%8;
	lv=100;
	PORTA = diem;
}
//----------------------------------------------------------------
/* using buttons to make the bar move left or right*/
void dich()
{
	if(trai==0)
	{	
		__delay_ms(25);
		if(trai==0) 
		{
			thanh=thanh<<1;
			if(thanh==0xC0) thanh=0xE0;
			PORTC=thanh;
		}	
	}
	if(phai==0)
	{
		__delay_ms(25);
		if(phai==0)
		{
		thanh=thanh>>1;
		if(thanh<0x07) thanh=0x07;
		PORTC=thanh;
		}
	} 
}
//----------------------------------------------------------------
/****************************************************************
* MAIN
****************************************************************/
/* -- void main (void)    -----------------------------------------
*
* Description    : Configure port,make the point falling and the bar moving  
*                    
* Parameters    : none
* Return        : don't care
* Notes        :display the bar,turn off the point then display the point,turn off the bar
*/

void main(void)
{
	system_init();
	while(1)
	{
		PORTB = 0x00;
		PORTD = 0x80;
		dich();
		PORTC = thanh;
		__delay_ms(10);

		PORTC = 0x00;
		PORTD = hang;
		i++;
		if(i==lv)
		{	
			hang = hang << 1;
			i=0;
		}
		if(hang == 0)
		{ 
			hang =0x01;
			cham=rand()%8;
			lv = lv -10;
			if (lv==0) lv=100;
		}
		if (hang==0x80)
		{
			j++;
		 	kq= thanh | PORTB;
			if (kq==thanh)
				if(j==1) diem=diem+1;
			PORTA = diem;
			kq=0;
		}
		PORTB = 0x01<<cham;
		__delay_ms(10);
	}
	while(1)
	{ 
		dich();
	}
	
}			
/****************************************************************
* END OF game.c
****************************************************************/